package cn.itcast.service.Impl;

import cn.itcast.feign.client.RoomClient;
import cn.itcast.feign.client.UserClient;
import cn.itcast.feign.pojo.*;
import cn.itcast.mapper.OrderMapper;
import cn.itcast.service.OrderService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;


@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserClient userClient;
    @Autowired
    private RoomClient roomClient;

    @Override
    public Order getOrderById(Integer orderId) {
        return orderMapper.selectById(orderId);
    }

    @Override
    public void createOrder(Order order) {
        User user = userClient.getById(order.getUserId());
        order.setUserName(user.getName());
        Room room = roomClient.select(order.getRoomId());
        order.setRoomPrice(room.getRoomPrice());
        orderMapper.insert(order);
    }

    @Override
    public PageBean getOrdersByIdCardAndRoomNumberAndDateRange(String userName, Integer roomPrice, Date startDate, Date endDate, Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        Page<Order> result = (Page<Order>) orderMapper.selectList(userName, roomPrice, startDate, endDate);
        return new PageBean(result.getTotal() ,result.getResult());
    }
}
